package com.dashunwms_aohe.job;

import com.alibaba.fastjson.JSONArray;
import com.dashunwms_aohe.bean.Qf_BillType;
import com.dashunwms_aohe.bean.ZxyUploadbillDS;
import com.dashunwms_aohe.bean.ds.*;
import com.dashunwms_aohe.dao.ZxyUploadbillDSDao;
import com.dashunwms_aohe.http.dto.*;
import com.dashunwms_aohe.service.BackWriteDataToQfService;
import com.dashunwms_aohe.service.DSBackWriteDataBaseService;
import com.dashunwms_aohe.service.GetDSDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Slf4j
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class GetDSDateJob {
    @Resource
    private GetDSDataService getDSDataService;
    @Resource
    private ZxyUploadbillDSDao zxyUploadbillDSDao;
    @Resource
    private DSBackWriteDataBaseService dsBackWriteDataBaseService;
    private String date0 = "2023-12-01";//开始时间
    private String date1 = "2099-12-21";//结束时间

    //TODO 获取验收和收货记录,这里默认如果查到验收记录了,证明已经可以查询到收货记录,所有收货和验收记录要一起去抓取
    @Scheduled(fixedDelay = 620000)//1000=1秒
    public void getDS_YSJLAndSHJL() {
        //TODO 获取入库相关数据(采购)
        List<ZxyUploadbillDS> buyBillCodeList = zxyUploadbillDSDao.getBackWriteBillByBilltypeDistinct(Qf_BillType.QF_Buy);
        for (ZxyUploadbillDS zxyUploadbillDS : buyBillCodeList) {
            log.info("拉取采购入库定时器");
            //TODO 获取采购验收记录
            LocalDate today = LocalDate.now();
            LocalDate yesterday = today.minusDays(1);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String beforeDate = yesterday.format(formatter);
            GetDS_YSJLDto getDSYsjlDto = new GetDS_YSJLDto();
            getDSYsjlDto.setDate0(date0);
            getDSYsjlDto.setDate1(date1);
            getDSYsjlDto.setIH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_InStockYSJLRes> dsInStockYSJLsList = getDSDataService.getDS_InStockYSJL(getDSYsjlDto);
            for (int i = 0; i < dsInStockYSJLsList.size(); i++) {
                DS_InStockYSJLRes dsInStockYSJLRes = dsInStockYSJLsList.get(i);
                dsInStockYSJLRes.setErpBillType("采购");
                dsInStockYSJLsList.set(i, dsInStockYSJLRes);

            }
            int ysjlCount_buy = dsBackWriteDataBaseService.insertYSJLToDataBase(dsInStockYSJLsList);
            //TODO 获取采购收货记录
            GetDS_SHJLDto getDSShjlDto = new GetDS_SHJLDto();
            getDSShjlDto.setDate0(date0);
            getDSShjlDto.setDate1(date1);
            getDSShjlDto.setRH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_InStockSHJLRes> dsInStockSHJLList = getDSDataService.getDS_InStockSHJL(getDSShjlDto);
            for (int i = 0; i < dsInStockSHJLList.size(); i++) {
                DS_InStockSHJLRes inStockSHJLRes = dsInStockSHJLList.get(i);
                inStockSHJLRes.setErpBillType("采购");
                dsInStockSHJLList.set(i, inStockSHJLRes);

            }
            int shjlCount_buy = dsBackWriteDataBaseService.insertSHJLToDataBase(dsInStockSHJLList);
            //TODO 获取拒收记录
            GetDS_JSJLDto getDSJsjlDto = new GetDS_JSJLDto();
            getDSJsjlDto.setDate0(date0);
            getDSJsjlDto.setDate1(date1);
            getDSJsjlDto.setIH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_InStockJSJLRes> dsInStockJSJLResList = getDSDataService.getDS_InStockJSJL(getDSJsjlDto);
            for (int i = 0; i < dsInStockJSJLResList.size(); i++) {
                DS_InStockJSJLRes inStockJSJLRes = dsInStockJSJLResList.get(i);
                inStockJSJLRes.setErpBillType("采购");
                dsInStockJSJLResList.set(i, inStockJSJLRes);
            }
            int jsjlCount_Buy = dsBackWriteDataBaseService.insertSHJSJLToDataBase(dsInStockJSJLResList);

            //TODO 获取不合格验收记录
            GetDS_BHGYSJLDto getDSBhgysjlDto = new GetDS_BHGYSJLDto();
            getDSBhgysjlDto.setIH_OWNER_NO(zxyUploadbillDS.getBillcode());
            getDSBhgysjlDto.setDate0(date0);
            getDSBhgysjlDto.setDate1(date1);
            List<DS_InStockBHGYSJLRes> dsInStockBHGYSJLRList = getDSDataService.getDS_InStockBHGYSJLR(getDSBhgysjlDto);
            for (int i = 0; i < dsInStockBHGYSJLRList.size(); i++) {
                DS_InStockBHGYSJLRes dsInStockBHGYSJLRes = dsInStockBHGYSJLRList.get(i);
                dsInStockBHGYSJLRes.setErpBillType("采购");
                dsInStockBHGYSJLRList.set(i, dsInStockBHGYSJLRes);
            }
            int bhgysjlCount_Buy = dsBackWriteDataBaseService.insertBHGYSJLToDataBase(dsInStockBHGYSJLRList);


        }
        //TODO 获取入库相关数据(销售退货)
        List<ZxyUploadbillDS> saleBackBillCodeList = zxyUploadbillDSDao.getBackWriteBillByBilltypeDistinct(Qf_BillType.QF_SaleBack);
        for (ZxyUploadbillDS zxyUploadbillDS : saleBackBillCodeList) {
            //TODO 获取销售退货验收记录
            GetDS_XTYSJLDto getDSXtysjlDto = new GetDS_XTYSJLDto();
            getDSXtysjlDto.setIH_OWNER_NO(zxyUploadbillDS.getBillcode());
            getDSXtysjlDto.setDate0(date0);
            getDSXtysjlDto.setDate1(date1);
            List<DS_InStockXTYSJLRes> dsInStockYSJLsList = getDSDataService.getDS_InStockXTYSJL(getDSXtysjlDto);
            for (int i = 0; i < dsInStockYSJLsList.size(); i++) {
                DS_InStockXTYSJLRes dsInStockXTYSJLRes = dsInStockYSJLsList.get(i);
                dsInStockXTYSJLRes.setErpBillType("销售退货");
                dsInStockYSJLsList.set(i, dsInStockXTYSJLRes);

            }
            int ysjlCount_saleBck = dsBackWriteDataBaseService.insertXTYSJLToDataBase(dsInStockYSJLsList);
            //TODO 获取销售退货收货记录
            GetDS_SHJLDto getDSShjlDto = new GetDS_SHJLDto();
            getDSShjlDto.setDate0(date0);
            getDSShjlDto.setDate1(date1);
            getDSShjlDto.setRH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_InStockSHJLRes> dsInStockSHJLList = getDSDataService.getDS_InStockSHJL(getDSShjlDto);
            for (int i = 0; i < dsInStockSHJLList.size(); i++) {
                DS_InStockSHJLRes inStockSHJLRes = dsInStockSHJLList.get(i);
                inStockSHJLRes.setErpBillType("销售退货");
                dsInStockSHJLList.set(i, inStockSHJLRes);

            }
            int shjlCount_saleBck = dsBackWriteDataBaseService.insertSHJLToDataBase(dsInStockSHJLList);
            //TODO 获取不合格验收记录
            GetDS_BHGYSJLDto getDSBhgysjlDto = new GetDS_BHGYSJLDto();
            getDSBhgysjlDto.setIH_OWNER_NO(zxyUploadbillDS.getBillcode());
            getDSBhgysjlDto.setDate0(date0);
            getDSBhgysjlDto.setDate1(date1);
            List<DS_InStockBHGYSJLRes> dsInStockBHGYSJLRList = getDSDataService.getDS_InStockBHGYSJLR(getDSBhgysjlDto);
            for (int i = 0; i < dsInStockBHGYSJLRList.size(); i++) {
                DS_InStockBHGYSJLRes dsInStockBHGYSJLRes = dsInStockBHGYSJLRList.get(i);
                dsInStockBHGYSJLRes.setErpBillType("销售退货");
                dsInStockBHGYSJLRList.set(i, dsInStockBHGYSJLRes);

            }
            int bhgysjlCount_saleBack = dsBackWriteDataBaseService.insertBHGYSJLToDataBase(dsInStockBHGYSJLRList);

        }

    }

    //TODO 获取出库相关数据
    @Scheduled(fixedDelay = 630000)//1000=1秒
    public void getDS_CKFH() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        log.info(dateFormat.format(date));
        //TODO 获取销售出库复核记录
        List<ZxyUploadbillDS> saleBillCodeList = zxyUploadbillDSDao.getBackWriteBillByBilltypeDistinct(Qf_BillType.QF_Sale);
        for (ZxyUploadbillDS zxyUploadbillDS : saleBillCodeList) {
            GetDS_CKFHDto getDSCkfhDto = new GetDS_CKFHDto();
            getDSCkfhDto.setPKH_OWNER_NO(zxyUploadbillDS.getBillcode());
            getDSCkfhDto.setDate0(date0);
            getDSCkfhDto.setDate1(date1);
            getDSCkfhDto.setPKH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_OutStockFHRes> dsOutStockFHList = getDSDataService.getDS_OutStockFH(getDSCkfhDto);
            for (int i = 0; i < dsOutStockFHList.size(); i++) {
                DS_OutStockFHRes dsOutStockFHRes = dsOutStockFHList.get(i);
                dsOutStockFHRes.setErpBillType("销售");
                dsOutStockFHList.set(i, dsOutStockFHRes);
            }
            int ckfhCount_sale = dsBackWriteDataBaseService.insertCKFHToDataBase(dsOutStockFHList);
        }
        //TODO 获取采购退货出库复核记录和采购退货记录
        List<ZxyUploadbillDS> buyBackBillCodeList = zxyUploadbillDSDao.getBackWriteBillByBilltypeDistinct(Qf_BillType.QF_BuyBack);
        for (ZxyUploadbillDS zxyUploadbillDS : buyBackBillCodeList) {
            GetDS_CKFHDto getDSCkfhDto = new GetDS_CKFHDto();
            getDSCkfhDto.setPKH_OWNER_NO(zxyUploadbillDS.getBillcode());
            getDSCkfhDto.setDate0(date0);
            getDSCkfhDto.setDate1(date1);
            List<DS_OutStockFHRes> dsBuyBackOutStockFHList = getDSDataService.getDS_OutStockFH(getDSCkfhDto);
            for (int i = 0; i < dsBuyBackOutStockFHList.size(); i++) {
                DS_OutStockFHRes dsOutStockFHRes = dsBuyBackOutStockFHList.get(i);
                dsOutStockFHRes.setErpBillType("采购退货");
                dsBuyBackOutStockFHList.set(i,dsOutStockFHRes);

            }
            int ckfhCount_cgth = dsBackWriteDataBaseService.insertCKFHToDataBase(dsBuyBackOutStockFHList);
            GetDS_JTOutStockJLDto getDSJtOutStockJLDto = new GetDS_JTOutStockJLDto();
            getDSJtOutStockJLDto.setDate0(date0);
            getDSJtOutStockJLDto.setDate1(date1);
            getDSJtOutStockJLDto.setPKH_OWNER_NO(zxyUploadbillDS.getBillcode());
            List<DS_JTOutStockJLRes> dsJtOutStockJLList = getDSDataService.getDS_JTOutStockJL(getDSJtOutStockJLDto);
            for (int i = 0; i < dsJtOutStockJLList.size(); i++) {
                DS_JTOutStockJLRes dsJtOutStockJLRes = dsJtOutStockJLList.get(i);
                dsJtOutStockJLRes.setErpBillType("采购退货");
                dsJtOutStockJLList.set(i,dsJtOutStockJLRes);

            }
            int cgthCount_cgth = dsBackWriteDataBaseService.insertCGTHJLToDataBase(dsJtOutStockJLList);

        }
    }

    //TODO 获取委托运输记录
    @Scheduled(fixedDelay = 670000)//1000=1秒
    public void getDS_WTYSJL() {
        List<ZxyUploadbillDS> billCodeList = zxyUploadbillDSDao.getUntreatedSYJL();
        for (ZxyUploadbillDS zxyUploadbillDS : billCodeList) {
            GetDS_WTYSJLDto getDSWtysjlDto = new GetDS_WTYSJLDto();
            getDSWtysjlDto.setLoh_owner_no(zxyUploadbillDS.getBillcode());
            List<DS_WTYSJLRes> dsWtysjlRes = getDSDataService.getDS_WTYSJLRes(getDSWtysjlDto);
            int wtysjlCount_sale = dsBackWriteDataBaseService.insertWTYSJLToDataBase(dsWtysjlRes);
        }
    }

    //TODO 获取库存
    //    @Scheduled(fixedDelay = 96000)//1000=1秒
    public void getDS_Stock() {
        GetDS_StockDto getDSStockDto = new GetDS_StockDto();
        List<DS_StockRes> dsStock = getDSDataService.getDS_Stock(getDSStockDto);
        int stockCount = dsBackWriteDataBaseService.insertStockToDataBase(dsStock);

    }

    //TODO 获取不合格库存
//    @Scheduled(fixedDelay = 99000)//1000=1秒
    public void getDS_BHGJL() {
        GetDS_StockBHGDto getDSStockDto = new GetDS_StockBHGDto();
        getDSStockDto.setDate0(date0);
        getDSStockDto.setDate1(date1);
        List<DS_StockBHGRes> dsStockBHGResList = getDSDataService.getDS_StockBHG(getDSStockDto);
        int stockCount = dsBackWriteDataBaseService.insertStockBHGToDataBase(dsStockBHGResList);

    }

    //TODO 获取库存养护记录
//    @Scheduled(fixedDelay = 98000)//1000=1秒
    public void getDS_YHJL() {
        GetDS_YHJLDto getDSYhjlDto = new GetDS_YHJLDto();
        getDSYhjlDto.setDate0(date0);
        getDSYhjlDto.setDate1(date1);
        List<DS_StockYHJLRes> dsStockYHJLs = getDSDataService.getDS_StockYHJL(getDSYhjlDto);
        int stockCount = dsBackWriteDataBaseService.insertStockYHJLToDataBase(dsStockYHJLs);

    }

}
